package com.qq.service;

import com.qq.common.Message;
import com.qq.common.MessageType;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * 功能：该类负责运行一个持续从服务器获取消息的线程
 *
 * @author 可凡
 */
public class ClientConnectServerThread extends Thread {
    private final Socket socket;

    /**
     * 功能：ClientConnectServerThread构造器，使线程持有一个Socket
     *
     * @param socket 登录对象的socket
     */
    public ClientConnectServerThread(Socket socket) {
        this.socket = socket;
    }

    public Socket getSocket() {
        return socket;
    }

    @Override
    public void run() {
        //通过socket获取输入对象，使用while循环持续接收消息
        ObjectInputStream objectInputStream;
        while (true) {
            try {
                //从socket获取对象输入流
                objectInputStream = new ObjectInputStream(socket.getInputStream());
                try {
                    //将类型转换为message
                    Message receiveMessage = (Message) objectInputStream.readObject();
                    //判断消息类型是否是返回在线用户类型，是的话就用空格来分割字符串得到数组并遍历
                    if (receiveMessage.getMesType().equals(MessageType.MESSAGE_RET_ONLINE_USERS)) {
                        System.out.println("当前在线用户为：");
                        String[] s = receiveMessage.getContent().split(" ");
                        for (String string : s) {
                            System.out.println("用户：" + string);
                        }
                    }
                    //判断接收消息类型是否是普通消息
                    if (receiveMessage.getMesType().equals(MessageType.MESSAGE_COMM_MES)) {
                        System.out.println("\n收到消息：");
                        System.out.println(receiveMessage.getDateTime().toString());
                        System.out.print(receiveMessage.getSender() + "：" + receiveMessage.getContent());
                    }
                    //判断接收消息类型是否是群发消息
                    if (receiveMessage.getMesType().equals(MessageType.MESSAGE_MASS_MES)) {
                        System.out.println("\n收到来自 " + receiveMessage.getSender() + " 的群发消息：");
                        System.out.println(receiveMessage.getContent());
                    }
                    //判断接收消息类型是否是发送文件类型
                    if (receiveMessage.getMesType().equals(MessageType.MESSAGE_SEND_TEXT_FILE_MES) || receiveMessage.getMesType().equals(MessageType.MESSAGE_SEND_BINARY_FILE_MES)) {
                        System.out.println("\n收到来自 " + receiveMessage.getSender() + " 发送的文件");
                        String path = "D:\\QQClient\\Download\\";
                        File file = new File(path);
                        if (!file.exists()) {
                            file.mkdirs();
                        }
                        if (receiveMessage.getMesType().equals(MessageType.MESSAGE_SEND_TEXT_FILE_MES)) {
                            String s = new String(receiveMessage.getData(), StandardCharsets.UTF_8);
                            FileWriter fileWriter = new FileWriter(path + receiveMessage.getContent());
                            fileWriter.write(s);
                            fileWriter.close();
                            System.out.println("文件保存成功！");
                        }
                        if (receiveMessage.getMesType().equals(MessageType.MESSAGE_SEND_BINARY_FILE_MES)) {
                            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(path + receiveMessage.getContent()));
                            bufferedOutputStream.write(receiveMessage.getData());
                            bufferedOutputStream.close();
                            System.out.println("文件保存成功！");
                        }
                    }
                } catch (ClassNotFoundException e) {
                    System.out.println("信息转换时出现错误" + e.getMessage());
                    break;
                }
            } catch (IOException e) {
                System.out.println("\n服务器关闭或该账号登出" + e.getMessage());
                break;
            }
        }
    }
}
